package com.example.p2pinvest.ui;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ScrollView;

import com.example.p2pinvest.util.UIUtils;

/**
 * @author h2tc01
 * @version 1.0.0
 * 2022/8/8
 * 自定义ViewGroup
 */
public class MyScrollView extends ScrollView {
    private View childView;

    public MyScrollView(Context context) {
        super(context);
    }

    public MyScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /*@Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }*/

    //用于获取子视图
    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        if (getChildCount() > 0) {
            childView = getChildAt(0);
        }
    }

    private int lastY;  //上一次Y轴方向操作的坐标位置
    private Rect normal = new Rect();  //用户记录布局原本的上下左右坐标
    private boolean isFinishAnimation = true; //判断动画是否结束

    private int lastX, downX, downY;

    //拦截,实现父视图对子视图的事件拦截
    //是否拦截成功,取决于返回值,true:拦截成功 false:拦截失败
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        boolean isIntercept = false;

        int eventX = (int) ev.getX();
        int eventY = (int) ev.getY();
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                lastX = downX = eventX;
                lastY = downY = eventY;
                break;
            case MotionEvent.ACTION_MOVE:
                //获取水平和垂直方向的移动距离,取绝对值
                int absX = Math.abs(eventX - downX);
                int absY = Math.abs(eventY - downY);

                if (absY > absX && absY >= UIUtils.dp2px(10)) {
                    //如果水平移动的距离大于垂直移动的距离,则判断用户要上下拉动视图,则拦截事件
                    isIntercept = true;
                }

                lastX = eventX;
                lastY = eventY;
                break;
        }
        return isIntercept;
    }


    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if (childView == null || !isFinishAnimation) {
            return super.onTouchEvent(ev);
        }
        int y = (int) ev.getY();  //获取当前的Y轴坐标
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                lastY = y;
                break;
            case MotionEvent.ACTION_MOVE:
                int dy = y - lastY;  //计算移动量
                if (isNeedMove()) {
                    //超出父类临界范围中,按照自定义的方式滑动

                    //记录原始的布局坐标
                    if (normal.isEmpty()) {
                        //只赋值一次,且为临界值
                        normal.set(childView.getLeft(), childView.getTop(), childView.getRight(), childView.getBottom());
                    }

                    //重新布局
                    childView.layout(childView.getLeft(), childView.getTop() + dy / 2, childView.getRight(), childView.getBottom() + dy / 2);
                }

                lastY = y;  //重新赋值
                break;
            case MotionEvent.ACTION_UP:
                if (!normal.isEmpty()) {  //当normal不为空时,表名移动超过临界值,需要执行平移动画
                    //使用平移动画
                    int transLateY = childView.getBottom() - normal.bottom;
                    TranslateAnimation translateAnimation = new TranslateAnimation(0, 0, 0, -transLateY);
                    translateAnimation.setDuration(200);
                    //translateAnimation.setFillAfter(true);  //停留在最终位置上

                    translateAnimation.setAnimationListener(new Animation.AnimationListener() {
                        @Override
                        public void onAnimationStart(Animation animation) {
                            isFinishAnimation = false;
                        }

                        @Override
                        public void onAnimationEnd(Animation animation) {
                            isFinishAnimation = true;
                            childView.clearAnimation();  //清除动画
                            childView.layout(normal.left, normal.top, normal.right, normal.bottom);  //布局变回原来的临界状态
                            //清空normal中的坐标值
                            normal.setEmpty();
                        }

                        @Override
                        public void onAnimationRepeat(Animation animation) {
                        }
                    });

                    childView.startAnimation(translateAnimation);
                }
                break;
        }
        return super.onTouchEvent(ev);
    }

    private boolean isNeedMove() {
        //获取子视图的高度
        int childHeight = childView.getMeasuredHeight();

        int height = this.getMeasuredHeight();  //获取布局的高度

        int dy = childHeight - height;  // dy >= 0

        int scrollY = this.getScrollY();  //获取用户在y轴方向上的偏移量(上 + 下 -)
        if (scrollY <= 0 || scrollY >= dy) {
            return true;  //按照自定义的方式处理上下滚动
        }
        //其他情况下都是处于临界范围内的,即表示仍按照ScrollView的方式处理上下滚动
        return false;
    }
}
